package com.yn.service.zy.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yn.common.enums.BooleanEnum;
import com.yn.common.model.zy.TDeviceInfoDO;
import com.yn.common.page.BasePage;
import com.yn.common.request.sc.PageParamRequest;
import com.yn.service.dao.zy.TDeviceInfoMapper;
import com.yn.service.zy.TDeviceInfoService;
import com.yn.service.zy.model.dto.TDeviceInfoDTO;
import com.yn.service.zy.model.dto.TDeviceInfoInDTO;
import com.yn.service.zy.model.query.TDeviceInfoQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class TDeviceInfoServiceImpl extends ServiceImpl<TDeviceInfoMapper, TDeviceInfoDO> implements TDeviceInfoService {

    @Resource
    private TDeviceInfoMapper tDeviceInfoMapper;

    @Override
    public IPage<TDeviceInfoDTO> listPage(TDeviceInfoQuery query, BasePage basePage) {
        IPage<TDeviceInfoDO> page = new Page(basePage.getPage(),basePage.getLimit());

        LambdaQueryWrapper<TDeviceInfoDO> queryWrapper = new LambdaQueryWrapper<>();

        IPage<TDeviceInfoDO> page1 = tDeviceInfoMapper.selectPage(page, queryWrapper);

        return page1.convert(TDeviceInfoDTO::transform);
    }

    @Override
    public boolean delete(Integer deviceInfoId) {
        TDeviceInfoDO deviceInfoDO = tDeviceInfoMapper.selectById(deviceInfoId);
        if (Objects.isNull(deviceInfoDO)){
            return true;
        }
        deviceInfoDO.setIsDelTag(BooleanEnum.YES.getCode());
        return this.updateById(deviceInfoDO);
    }

    @Override
    public BasePage<TDeviceInfoDTO> listPage1(TDeviceInfoQuery query, PageParamRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPage(), pageRequest.getLimit());
        LambdaQueryWrapper<TDeviceInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        List<TDeviceInfoDO> tDeviceInfoDOS = baseMapper.selectList(queryWrapper);
        if(CollUtil.isEmpty(tDeviceInfoDOS)){
            return BasePage.restPage(new ArrayList<>());
        }
        BasePage<TDeviceInfoDO> basePage = BasePage.restPage(tDeviceInfoDOS);
        ArrayList<TDeviceInfoDTO> arrayList = new ArrayList<>(tDeviceInfoDOS.size());
        for (TDeviceInfoDO tDeviceInfoDO : tDeviceInfoDOS) {
            arrayList.add(TDeviceInfoDTO.transform(tDeviceInfoDO));
        }
        BasePage<TDeviceInfoDTO> tDeviceInfoDTOBasePage = BasePage.restPage(arrayList);
        BeanUtils.copyProperties(basePage,tDeviceInfoDTOBasePage,"list");
        return tDeviceInfoDTOBasePage;
    }
}
